﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calligraphy.CalligraphyData
{
    partial class T_Rubbing
    {

        private Uri _photo110;
        public Uri Photo110
        {
            get
            {
                if (_photo110 == null)
                {
                    LoadSmallPhotoEntities();
                }
                return _photo110;
            }           
        }

        private void LoadSmallPhotoEntities()
        {
            if (T_Photo == null)
                return;
            if (T_Photo.T_Photo1.Count > 0)
                return;
            var q = from sp in DataServiceContextHelper.DataContext.T_Photo where sp.RawPhotoID == T_Photo.ID && (sp.IsDeleted == null || sp.IsDeleted == false) select sp;
            T_Photo.T_Photo1.LoadAsync(q);
            T_Photo.T_Photo1.LoadCompleted += T_Photo1_LoadCompleted;
        }

        void T_Photo1_LoadCompleted(object sender, System.Data.Services.Client.LoadCompletedEventArgs e)
        {
            LoadPhoto110Uri();
        }

        private async void LoadPhoto110Uri()
        {
            T_Photo smallphotoEntity = PhotoLoader.GetSmallPhoto(T_Photo, PhotoLoader.SmallPhotoSizes110);
            if (smallphotoEntity != null)
            {
                _photo110 =
                    await PhotoLoader.GetImageUriAsync(smallphotoEntity);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Photo110"));
                }
            }
        }

        public string CustromDescription
        {
            get
            {
                string str = string.Empty;
                if (T_Dynast != null)
                {
                    str += T_Dynast.Name.Trim();
                    str += "拓";
                }

                if (!string.IsNullOrEmpty(Location))
                {
                    if(!string.IsNullOrEmpty(str))
                        str += "\t";
                    str += Location.Trim();
                }
                if (!string.IsNullOrEmpty(SubDescription))
                {
                    if (!string.IsNullOrEmpty(str))
                        str += "\r\n";
                    str += SubDescription.Substring(100);
                }
                return str;
            }
        }
    }
}
